package com.stylefeng.guns.rest.modular.auth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.stylefeng.guns.api.exception.GunsException;
import com.stylefeng.guns.api.user.UserApi;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.modular.auth.controller.dto.AuthRequest;
import com.stylefeng.guns.rest.modular.auth.controller.dto.AuthResponse;
import com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil;
import com.stylefeng.guns.rest.modular.auth.validator.IReqValidator;
import com.stylefeng.guns.rest.modular.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 请求验证的
 *
 * @author fengshuonan
 * @Date 2017/8/24 14:22
 */
@RestController
public class AuthController {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

//    @Resource(name = "simpleValidator")
//    private IReqValidator reqValidator;

    @Reference(interfaceClass = UserApi.class)
    private UserApi userApi;

    @RequestMapping(value = "${jwt.auth-path}")
    public ResponseVo<?> createAuthenticationToken(AuthRequest authRequest) {
//        boolean validate = reqValidator.validate(authRequest);
        System.out.println("  用户名 --> "+authRequest.getUserName()+"    密码 --> "+authRequest.getPassword());
        //userApi.isLogin(authRequest.getUserName(), authRequest.getPassword());
        //使用我们自己的验证方式
        boolean validate = true;
        int login = userApi.isLogin(authRequest.getUserName(), authRequest.getPassword());
        if (login ==0){
            validate = false;
        }


        if (validate) {
            final String randomKey = jwtTokenUtil.getRandomKey();
            final String token = jwtTokenUtil.generateToken(""+login, randomKey);

            return ResponseVo.success(new AuthResponse(token, randomKey));
//            return ResponseEntity.ok(new AuthResponse(token, randomKey));
        } else {
            return ResponseVo.serviceFail("用户名或密码错误");
            //throw new GunsException(BizExceptionEnum.AUTH_REQUEST_ERROR);
        }
    }
}
